<template>
  <div class="div_padding">
    <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline" size="small">
      <el-form-item label="部门名称：" prop="name">
        <el-input v-model="formInline.name" placeholder="部门名称"></el-input>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-select v-model="formInline.status" placeholder="机构状态">
          <el-option label="正常" value='True'></el-option>
          <el-option label="停用" value='False'></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" v-permission="'Search'" type="primary" @click="filterData">查询</el-button>
        <el-button icon="el-icon-refresh" plain @click="resetForm('formInline')">重置</el-button>
        <el-button icon="el-icon-plus" v-permission="'Create'" type="success" @click="onSubmit">添加机构</el-button>
        <Insert ref="insert" v-bind:deptData="tableData" @reloadTable="reloadTable"></Insert>
        <Edit ref="edit" v-bind:deptData="tableData" @reloadTable="reloadTable"></Edit>
      </el-form-item>
      <el-button style="float: right" icon="el-icon-refresh-right" circle @click="reloadTable"></el-button>
    </el-form>
    <el-table
      :data="tableData"
      size="mini"
      height="78vh"
      v-loading="loading"
      empty-text="查询结果为空!"
      element-loading-text="数据加载中..."
      element-loading-spinner="el-icon-loading"
      style="width: 100%;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
        prop="id"
        label="ID"
        v-if="showId"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="部门名称"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="dept_id"
        label="部门ID"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="owner"
        label="负责人"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="phone"
        label="电话"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="email"
        label="Email"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
      prop="sort"
      label="排序"
      align="center"
      sortable
      width="100">
    </el-table-column>
      <el-table-column prop="status" width="100" align="center" label="显示状态">
      <template slot-scope="scope">
        <span v-if="scope.row.status == true" style="color:#67C23A;">正常</span>
        <span v-if="scope.row.status == false" style="color:#909399;">停用</span>
      </template>
      </el-table-column>
      <el-table-column
        prop="update_datetime"
        label="更新时间"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="create_datetime"
        label="创建时间"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作" align="center" width="250" prop="parent" fixed="right">
        <template slot-scope="scope">
            <el-button type="primary" v-permission="'Update'" size="mini" @click="onEdit(scope.$index,scope.row)" icon="el-icon-edit" plain>修改</el-button>
            <el-button type="danger" v-permission="'Delete'" size="mini" icon="el-icon-delete" plain @click="onDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
  import { getDeptData,delDeptData } from '@/api/dept'
  import { successMsg } from '@/utils/tools'
  import Insert from './insert'
  import Edit from './edit'
  export default {
    name:'Dept',
    components:{
      Insert,
      Edit
    },
    data() {
      return {
        showId:false,
        loading:true,
        tableData: [],
        formInline: {
          name: '',
          status: ''
        }
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        getDeptData().then(res=>{
          this.tableData=this.$utils.toArrayTree(res.data.data,{ parentKey: 'parent', strict: true })
          this.loading=false
        })
      },
      onSubmit(){
        this.$refs.insert.dialogFormVisible = true
      },
      onEdit(index,row){
        this.$refs.edit.dialogFormVisible = true
        this.$refs.edit.formInline = Object.assign({}, row);//formInline，将row的数据复制到editForm对象中，实现的是浅拷贝
      },
      onDelete(row){
        this.$confirm('此操作将真实删除该机构,谨慎操作!是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          delDeptData(row.id).then(res=>{
            if(res.code===2000) {
              successMsg(res.msg)
              this.reloadTable()
            }
          })
        }).catch(()=>{
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      },
      reloadTable(){
        this.loading=true
        this.fetchData()
      },
      filterData(){
        let params = {}
        if(this.formInline.name){
          params.name=this.formInline.name
        }
        if(this.formInline.status){
          params.status=this.formInline.status
        }
        this.loading=true
        getDeptData(params).then(res=>{
          this.tableData=this.$utils.toArrayTree(res.data.data,{ parentKey: 'parent', strict: false })
          this.loading=false
        })
      },
      resetForm(formName){
        this.$refs[formName].resetFields()
        this.reloadTable()
      }
    },
  }
</script>
<style lang="scss" scoped>
  .div_padding {
    padding: 10px 10px
  }
</style>
